package com.it.you.base.digui;

import java.util.Stack;

public class DiGuiTest1 {

    public static void main(String[] args) {
        System.out.println(sumCircle(1, 100));
        System.out.println(sumRecursive(1,100));
        System.out.println(sumNonRecursive(1,100));

    }

    private static int sumCircle(int min, int max) {
        int sum = 0;
        for (int i = min; i <= max; i++) {
            sum += i;
        }
        return sum;
    }

    private static int sumRecursive(int min, int max) {
        // 边界条件
        if (min >= max) {
            return min;
        }
        // 问题规模缩小
        int sum = sumRecursive(min, max - 1);
        // 加上当前值
        sum += max;
        // 返回
        return sum;
    }

    private static int sum(int min, int max) {
        return min >= max ? min : sum(min, max - 1) + max;
    }

    private static int sumNonRecursive(int min, int max) {
        int sum = 0;
        // 声明一个栈
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(max);
        while (!stack.isEmpty()) {
            if (max > min) {
                // 要计算max，先计算max-1
                stack.push(--max);
            } else {
                // 问题规模缩小到一定程度，计算返回
                sum += stack.pop();
            }
        }
        return sum;
    }
}
